<?php

class Publish_ReplyController extends Ext_Controller_Action_Front
{
    public function init()
    {
        parent::init();

    }
    
    public function savePostAction()
    {
        $viewResult = new Ext_View_Result();
        
        $destArticleId = $this->filter->filter($this->_request->get('destArticleId'));
        $content       = $this->filter->filter($this->_request->get('content'));
        
        $validater = new Ext_Validate();
        $validater->addValidater(new Add_Publish_Reply_SavePost_Validate_Acl(), true);
        $validater->addValidater(new Add_Publish_Reply_SavePost_Validate_Misc($destArticleId));
        $validater->addValidater(new Add_Publish_Reply_SavePost_Validate_Content($content));
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        
        $replyArticleId      = $destArticleId;
        $firstReplyArticleId = $destArticleId;
        $destArticle = App_Model_DbTable_Article::getInstance()->getRowById($destArticleId);
        if($destArticle){
            if($destArticle['firstReplyArticleId']){
                $firstReplyArticleId = $destArticle['firstReplyArticleId'];
            }
        }
        $row = array(
            'userId'              => $userId,
            'replyArticleId'      => $replyArticleId,
            'firstReplyArticleId' => $firstReplyArticleId,
            'postTime'            => time(),
            'editTime'            => time(),
        );                
        $articleId = App_Model_DbTable_Article::getInstance()->addNew($row);
        $row = array(
            'content'        => $content,
            'htmlOn'         => 0,
        );
        App_Model_DbTable_Article_Desc::getInstance()->add($row, $articleId);
        $row = array(
            'articleId'     => $articleId,
            'userId'        => $userId,
            'destArticleId' => $destArticleId,
        );
        App_Model_DbTable_Reply::getInstance()->addNew($row);
        $row = array(
            'replies' => 'replies+1',
        );
        App_Model_DbTable_Article::getInstance()->addOld($row, $destArticleId);         
        $articleSearchThreads = App_Model_DbTable_Article_Search_Threads::getInstance()->getRowByArticleId($articleId);
        if(!$articleSearchThreads){
            $row = array(
                'articleId' => $articleId,
                'userId'    => $userId,
            );
            App_Model_DbTable_Article_Search_Threads::getInstance()->addNew($row);
        }
        
        $viewResult->addMessage($this->translater->_('Submit success.'));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
}

